package user;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class User {

	protected String username;
	protected InetAddress address;
	
	/**
	 * @param username
	 * @param address
	 */
	public User(String username, InetAddress addr) {
		this.username = username;
		this.address = addr ;
	}
	
	/**
	 * @param username
	 * @param hostname
	 * @throws UnknownHostException if no host is found for the specified hostname
	 */
	public User(String username, String hostname) throws UnknownHostException {
		this.username = username;
		this.address = InetAddress.getByName(hostname);
	}

	/**
	 * @param user
	 */
	public User(User user) {
		this (user.username, user.address) ;
	}

	/**
	 * @return the user username
	 */
	public String getUsername() {
		return this.username;
	}
	
	/**
	 * @param host
	 * @return A unique identifier (ip)
	 */
	public static String createIdentifier (InetAddress host) {
		return host.getHostAddress();
	}

	/**
	 * @return A unique identifier (ip)
	 */
	public String getIdentifier () {
		return createIdentifier(address) ;
	}
	
	/**
	 * @return the user address
	 */
	public InetAddress getAddress() {
		return this.address;
	}
	
	/**
	 * Check if the 2 users are equals, i.e they have the same host address
	 * 
	 * @param other
	 * @return true if the 2 users are equals
	 */
	@Override
	public boolean equals (Object other) {
		if (other != null && other instanceof User) {
			return this.address.equals(((User) other).address) ;
		}
		else {
			return false ;
		}
	}
	
	@Override
	public String toString () {
		return "User (" + this.username + "@" + this.address.getHostAddress() + ")" ;
	}

}
